﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using VoteMS.DAL;
using VoteMS.Model;

/*
 * @author zhu95
 */
namespace VoteMS.BLL
{
    public class ProjectsBLL
    {

        private readonly  ProjectsDAL dal = new ProjectsDAL();
        /// <summary>
        /// 添加项目
        /// </summary>
        /// <returns></returns>
        public string AddProject(ProjectModel model)
        {
            if (string.IsNullOrEmpty(model.ProjectName))
            {
                return "项目名称不能为空|0";
            }
            if (model.TypeID <= 0)
            {
                return "项目类型不能为空|0";
                 
            }
            if (string.IsNullOrEmpty(model.ProjectDepart))
            {
                return "项目所属部门不能为空|0";
                 
            }
            if (string.IsNullOrEmpty(model.ProjectImage))
            {
                return "项目图片不能为空|0";
                 
            }
            if (string.IsNullOrEmpty(model.ProjectInfo))
            {
                return "项目简介不能为空|0";
                 
            }
            if (model.ProjectNumber != 0)
            {
                return "项目得票数初始必须为0|0";
                 
            }
            int number = dal.AddProject(model);
            if (number > 0)
            {
                return "项目添加成功|1";
            }else if (number == 0)
            {
                return "项目添加失败|0";
            }
            else
            {
                return "项目添加错误|-1";
            }
        }

        /// <summary>
        /// type1 type2 type3 否则为全部
        /// </summary>
        /// <param name="type">1,2,3，others</param>
        /// <returns></returns>
        public string GetProjectListJson(int type)
        {
            string filter = "";
            if (type == 1)
            {
                filter = "TypeID=1";
            }
            else if (type == 2)
            {
                filter = "TypeID=2";
            }
            else if (type == 3)
            {
                filter = "TypeID=3";
            }
            DataSet ds = dal.GetProjectsAll();
            DataView dv = ds.Tables[0].DefaultView;
            dv.RowFilter = filter;
            DataTable dt = dv.ToTable();
            string json = DataTableToJsonWithJavaScriptSerializer(dt);
            return json;
        }

        #region 删除

        public string DeleteProjectById(int pid)
        {
            if (pid <= 0)
            {
                return "请选择一个项目|0";
                //TODO JSON  "{"message":"请选择项目","code":0}"
            }
            int number = dal.DeleteProjectById(pid);
            if (number > 0)
            {
                return "删除项目成功|1";
            }
            if (number == 0)
            {
                return "删除项目失败|0";
            }

            return "删除项目出错|-1";
        }

        #endregion

        #region 修改
        public string UpdateProject(ProjectModel model)
        {
            if (string.IsNullOrEmpty(model.ProjectName))
            {
                return "项目名称不能为空|0";
            }
            if (model.TypeID <= 0)
            {
                return "项目类型不能为空|0";
            }
            if (string.IsNullOrEmpty(model.ProjectDepart))
            {
                return "项目所属部门不能为空|0";
            }
            if (string.IsNullOrEmpty(model.ProjectInfo))
            {
                return "项目简介不能为空|0";
            }
            int number = dal.UpdateProject(model);
            if (number > 0)
            {
                return "项目修改成功|1";
            }
            else if (number == 0)
            {
                return "项目修改失败|0";
            }
            else
            {
                return "项目修改错误|-1";
            }
        }

        /// <summary>
        /// 根据项目编号给项目得票数+1
        /// </summary>
        /// <param name="pid">项目编号</param>
        /// <returns></returns>
        public int UpdateProjectNumber(int pid)
        {
            return dal.UpdateProjectNumber(pid);
        }
        #endregion

        #region 查询

        public ProjectModel GetProjectById(int projectId)
        {
            return dal.GetProjectById(projectId);
        }
        public DataSet GetProjectByWhere(string strWhere)
        {
            return dal.GetProjectByWhere(strWhere);
        }


        #endregion


        //TODO DataTable to Json
        public static string DataTableToJsonWithJavaScriptSerializer(DataTable table)
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
            Dictionary<string, object> childRow;
            foreach (DataRow row in table.Rows)
            {
                childRow = new Dictionary<string, object>();
                foreach (DataColumn col in table.Columns)
                {
                    childRow.Add(col.ColumnName, row[col]);
                }
                parentRow.Add(childRow);
            }
            return jsSerializer.Serialize(parentRow);
        }

    }
}
